<?php

Class CommitteesController extends AppController {

    public $name = 'Committees';

    public function add($follow_id) {
        $this->set('follow_id', $follow_id);
        if (empty($this->data)) {
            
        } else {

            if ($this->Committee->save($this->data)) {
                $this->Session->setFlash('Registro Adicionado correctamente', 'flash_custom');
                $this->redirect(array('controller' => 'Committees', 'action' => 'index', $follow_id));
            } else {
                $this->Session->setFlash('Error Guardando datos');
            }
        }
    }

    public function edit($id) {
        $this->Committee->recursive = -1;
        if (empty($this->data)) {

            $this->data = $this->Committee->find('first', array('conditions' => array('Committee.id' => $id)));
        } else {

            if ($this->Committee->save($this->data)) {
                $this->Session->setFlash('Registro editado correctamente', 'flash_custom');
                $this->redirect(array('controller' => 'Committees', 'action' => 'index', $this->data['Committee']['follow_id']));
            } else {
                $this->Session->setFlash('Error editando datos');
            }
        }
    }

    public function index($follow_id) {
        $this->set('follow_id', $follow_id);
        $this->loadModel('Budget');
        if ($this->Budget->find('all', array('recursive' => -1, 'conditions' => array('Budget.follow_id' => $follow_id), 'fields' => array('Budget.id')))) {
            if ($this->Budget->find('all', array('recursive' => -1, 'conditions' => array('Budget.follow_id' => $follow_id, 'Budget.cerrado' => 0), 'fields' => array('Budget.id')))) {

                $this->Session->setFlash('No se ha cerrado el plan de inversión', 'flash_custom');
                $this->redirect(array('controller' => 'Follows', 'action' => 'index'));
            } else {
                $this->paginate = array('Committee' => array('maxLimit' => 500, 'limit' => 50, 'fields' => array('Committee.id', 'Committee.fecha', 'Committee.id')));
                $this->set('Committees', $this->paginate(array('Committee.follow_id' => $follow_id)));
            }
        }else{
            $this->Session->setFlash('No se ha agregado un plan de inversión ', 'flash_custom');
                $this->redirect(array('controller' => 'Follows', 'action' => 'index'));
        }
    }

    public function delete($comite_id, $follow_id) {
        if ($this->Committee->CommitteeBudget->deleteAll(array('CommitteeBudget.committee_id' => $comite_id))) {
            if ($this->Committee->delete($comite_id)) {
                $this->Session->setFlash('Registro eliminado correctamente', 'flash_custom');
                $this->redirect(array('controller' => 'Committees', 'action' => 'index', $follow_id));
            }
        }
    }

}

?>